package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import xml.XMLDOMPARSER;
import View.WindowMenu;
import View.WindowSimu;
import eNodeB.Fabrique;

public class Controller implements ActionListener, MouseListener, ErrorHandler {
	private final WindowMenu f;
	private WindowSimu fs;
	private final Model m;
	private boolean isValid = true;
	private static Controller singleton;

	public static Controller getController() {
		if (singleton == null) {
			singleton = new Controller();
		}
		return singleton;

	}

	private Controller() {
		m = new Model();
		f = new WindowMenu(this, m);
	}

	public Model getModel() {
		return m;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() instanceof JButton) {
			JButton btn = (JButton) arg0.getSource();
			String command = btn.getActionCommand();

			if (command.equals("Check")) {
				m.check();

			} else if (command.equals("Pause")) {
				try {
					synchronized (this) {
						Fabrique.getFabrique().getEnodeB().getHorloge().wait();
					}

				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (IllegalMonitorStateException e) {
				}
				fs.getPanel().replacePauseButton();
			} else if (command.equals("Restart")) {
				try {
					synchronized (this) {
						Fabrique.getFabrique().getEnodeB().getHorloge()
								.notify();
					}
				} catch (IllegalMonitorStateException e) {
					// System.out.println("Restarted Fail");
				}
				fs.getPanel().replaceRestartButton();
			} else if (command.equals("Start")) {
				Thread t = new Thread(Fabrique.getFabrique().getEnodeB()
						.getHorloge());
				t.start();
				fs.getPanel().replaceRestartButton();
			}
		} else if (arg0.getSource() instanceof JFileChooser) {
			JFileChooser jFC = (JFileChooser) arg0.getSource();
			if (JFileChooser.CANCEL_SELECTION.equals(arg0.getActionCommand())) {
				jFC.setVisible(false);
				jFC.removeAll();
			} else {

				String chemin = jFC.getSelectedFile().getAbsolutePath();
				boolean returnXML;
				try {
					isValid = true;
					returnXML = XMLDOMPARSER.getXMLDOMPARSER().parse(chemin,
							f.getPanel().getID());
					if (!returnXML) {
						throw new IllegalArgumentException("bad ID");
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Impossible to load XML FILE: " + e.getMessage());
					isValid = false;
				}
				if (isValid) {
					fs = new WindowSimu(this, m);

					f.setVisible(false);

				}

			}
		}

	}

	@Override
	public void warning(SAXParseException e) throws SAXException {
		JOptionPane.showMessageDialog(f,
				"Warning to load XML FILE: " + e.getMessage(), "Warning",
				JOptionPane.WARNING_MESSAGE);
		System.exit(1);

	}

	@Override
	public void error(SAXParseException e) throws SAXException {
		JOptionPane.showMessageDialog(f,
				"Error to load XML FILE: " + e.getMessage(), "Error",
				JOptionPane.ERROR_MESSAGE);
		isValid = false;

	}

	@Override
	public void fatalError(SAXParseException e) throws SAXException {
		JOptionPane.showMessageDialog(f,
				"Error to load XML FILE: " + e.getMessage(), "Error",
				JOptionPane.ERROR_MESSAGE);

		isValid = false;

	}
}
